package com.mxx.common.payment.local;

import com.mxx.common.payment.*;

import java.util.Map;

public class DefaultLocalPaymentHandler implements PaymentHandler<LocalPrepaymentResponse> {
    private SelectPayment selectPayment;

    public DefaultLocalPaymentHandler(SelectPayment selectPayment) {
        this.selectPayment = selectPayment;
    }

    @Override
    public LocalPrepaymentResponse prepaymentProcess(TradeMark tradeMark) {
        return new LocalPrepaymentResponse().setLocal(true).setState(0);
    }

    @Override
    public void paymentFallback(FinishTradeHandler paymentHandler, Map<String, String> params) {

    }

    @Override
    public PayResponse close(String orderNo) {
        return new PayResponse().setOutTradeNo(orderNo).setTradeState("SUCCESS").setTradeStateDesc("订单已关闭");
    }

    @Override
    public void refund(FinishTradeHandler finishTradeHandler, String outTradeNo, String totalAmount, String refundAmount, String refundReason) {
        finishTradeHandler.refundSuccessfully(TradeType.Local, outTradeNo, null, "已退款");
    }

    @Override
    public boolean analysisOfRefund(FinishTradeHandler paymentHandler, Map<String, String> params) {
        return true;
    }

    @Override
    public void selectPayment(FinishTradeHandler finishTradeHandler, String outTradeNo) {
        boolean pay = selectPayment.select(outTradeNo);
        if (pay) {
            finishTradeHandler.paymentSuccessfully(TradeType.Local, outTradeNo, null);
        } else {
            finishTradeHandler.paymentFailure(TradeType.Local, outTradeNo, null, "Close", "订单已关闭");
        }

    }

    @Override
    public PayResponse selectOfRefund(String outTradeNo, String operatorId) {
        return new PayResponse().setOutTradeNo(outTradeNo).setTradeState("SUCCESS").setTransactionId(operatorId).setTradeStateDesc("订单已退款");
    }

    @Override
    public TradeType getTradeType() {
        return TradeType.Local;
    }



    public interface SelectPayment {

        boolean select(String tradeNo);
    }
}
